#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
    // Check for command line args
    if (argc != 2)
    {
        printf("Usage: ./read infile\n");
        return 1;
    }

    // Create buffer to read into
    char buffer[7];

    // Create array to store plate numbers
    char *plates[8];

    FILE *infile = fopen(argv[1], "r");
    if(infile == NULL){
        printf("The file can not open!\n");
        return 2;
    }

    int idx = 0;
    while(idx < 8){
        if(fread(buffer, 1, 7, infile) != 7){
            break;
        }

        buffer[6] = '\0';
        plates[idx] = malloc(7 * sizeof(char));

        if(plates[idx] == NULL){
            printf("Memory allocation failed.\n");
            fclose(infile);
            for (int i = 0; i < idx; i++)
            {
                free(plates[i]);
            }
            return 3;
        }

        strcpy(plates[idx], buffer);
        idx++;

    }


    fclose(infile);

    for (int i = 0; i < idx; i++)
    {
        printf("%s\n", plates[i]);
        free(plates[i]); // Free the allocated memory
    }

    return 0;
}
